<?php

namespace app\api\controller;

use app\api\model\Article as ArticleModel;
use app\api\model\article\Category as CategoryModel;
use think\Db;
/**
 * 文章控制器
 * Class Article
 * @package app\api\controller
 */
class Article extends Controller
{
    /**
     * 文章首页
     * @return array
     */
    public function index()
    {
        // 文章分类列表
        $categoryList = CategoryModel::getAll();
        return $this->renderSuccess(compact('categoryList'));
    }
    public function wcjs()
    {
        $host = request()->domain(); // 获取当前域名，如 https://example.com
    
        $categoryList = Db::name("article_category")
            ->alias('c')
            ->join('upload_file f', 'c.image_id = f.file_id', 'LEFT')
            ->whereIn('c.category_id', [10007, 10009, 10010, 10011])
            ->order('c.sort asc')
            ->field('c.*, f.file_name')
            ->select()->toArray();
    
        // 拼接完整图片路径
        foreach ($categoryList as &$item) {
            $item['image_url'] = $item['file_name'] ? $host . '/uploads/' . $item['file_name'] : '';
        }
    
        return $this->renderSuccess(compact('categoryList'));
    }
    public function zksb()
    {
        $host = request()->domain(); // 获取当前域名，如 https://example.com
    
        $categoryList = Db::name("article_category")
            ->alias('c')
            ->join('upload_file f', 'c.image_id = f.file_id', 'LEFT')
            ->whereIn('c.category_id', [10008, 10012, 10013, 10014])
            ->field('c.*, f.file_name')
            ->select()->toArray();
    
        // 拼接完整图片路径
        foreach ($categoryList as &$item) {
            $item['image_url'] = $item['file_name'] ? $host . '/uploads/' . $item['file_name'] : '';
        }
    
        return $this->renderSuccess(compact('categoryList'));
    }
    /**
     * 文章列表
     * @param int $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($category_id = 0)
    {
        $model = new ArticleModel;
        $domain = request()->domain(); // 动态获取当前域名
    
        // 构建查询
        $query = $model
            ->alias('a')
            ->field([
                'a.*',
                'uf.file_name as image_file_name'
            ])
            ->join('upload_file uf', 'a.image_id = uf.file_id', 'LEFT') // 关联上传文件表
            ->with(['category'])
            ->where('a.article_status', '=', 1)
            ->where('a.is_delete', '=', 0);
    
        // 如果分类 ID 大于 0，则添加分类条件
        if ($category_id > 0) {
            $query->where('a.category_id', '=', $category_id);
        }
    
        // 获取分页数据
        $list = $query
            ->order(['a.article_sort' => 'asc', 'a.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
        // 如果是指定分类 ID，则处理图片
        if ($category_id == 10006) {
            foreach ($list as $key => $value) {
                // 如果有上传的图片，则拼接完整的 URL
                if (!empty($value['image_file_name'])) {
                    $list[$key]['image']['file_path'] = $domain . '/uploads/' . $value['image_file_name'];
                } else {
                    // 否则使用默认图片路径
                    switch ($value['article_id']) {
                        case '10013':
                            $list[$key]['image']['file_path'] = $domain . "/uploads/%E7%A4%BE%E4%BF%9D%E7%BC%B4%E8%B4%B9.png";
                            break;
                        case '10014':
                            $list[$key]['image']['file_path'] = $domain . "/uploads/%E5%8F%96%E5%8F%B7.png";
                            break;
                        default:
                            $list[$key]['image']['file_path'] = $domain . "/uploads/%E9%A2%84%E7%BA%A6%E6%8C%82%E5%8F%B7.png";
                            break;
                    }
                }
            }
        }else{
            // 对于其他分类，直接使用数据库中的图片路径
            foreach ($list as $key => $value) {
                if (!empty($value['image_file_name'])) {
                    $list[$key]['image']['file_path'] = $domain . '/uploads/' . $value['image_file_name'];
                } else {
                    $list[$key]['image'] = '';
                }
            }
        }
    
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 文章详情
     * @param $article_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($article_id)
    {
        $detail = ArticleModel::detail($article_id);
        return $this->renderSuccess(compact('detail'));
    }

}
